package edu.cibertec.sicsolutions.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

import edu.cibertec.sicsolutions.constants.SessionConstants;
import edu.cibertec.sicsolutions.exception.DomainException;
import edu.cibertec.sicsolutions.parameters.ParamerterVfs;
import edu.cibertec.sicsolutions.vfs.domain.votacion.ElectorVirtual;
import edu.cibertec.sicsolutions.vfs.domain.votacion.Empadronado;
import edu.cibertec.sicsolutions.vfs.domain.votacion.SessionTracking;
import edu.cibertec.sicsolutions.vfs.service.ElectorVirtualService;
import edu.cibertec.sicsolutions.vfs.service.EmpadronadoService;
import edu.cibertec.sicsolutions.vfs.service.SessionTrackingService;

public class ServletListener extends HttpServlet implements
		ServletContextListener, ServletContextAttributeListener,
		HttpSessionListener, HttpSessionAttributeListener,
		ServletRequestListener, ServletRequestAttributeListener {

	private static final Logger log = Logger.getLogger(ServletListener.class);
	
	// Notification that the web module is ready to process requests
	public void contextInitialized(ServletContextEvent sce) {
	/*	log.info("begin method contextInitialized()");
		log.info("end method contextInitialized()");
	*/	
		/*throw new java.lang.UnsupportedOperationException(
				"Method contextInitialized () not yet implemented.");*/
	}

	// Notification that the servlet context is about to be shut down
	public void contextDestroyed(ServletContextEvent sce) {
		
	/*	log.info("begin method contextDestroyed()");
		log.info("end method contextDestroyed()");
	*/	
		/*throw new java.lang.UnsupportedOperationException(
				"Method contextDestroyed () not yet implemented.");*/
	}

	// Notification that a new attribute has been added to the servlet context

	public void attributeAdded(ServletContextAttributeEvent scab) {
	/*	log.info("begin method attributeAdded()");
		log.info("end method attributeAdded()");
	*/	
	/*	throw new java.lang.UnsupportedOperationException(
				"Method attributeAdded () not yet implemented.");*/
	}

	// Notification that an attribute has been removed from the servlet context
	public void attributeRemoved(ServletContextAttributeEvent scab) {
	/*	log.info("begin method attributeRemoved()");
		log.info("end method attributeRemoved()");
	*/	
		/*throw new java.lang.UnsupportedOperationException(
				"Method attributeRemoved () not yet implemented.");*/
	}

	// Notification that an attribute of the servlet context has been replaced
	public void attributeReplaced(ServletContextAttributeEvent scab) {
	/*	log.info("begin method attributeReplaced()");
		log.info("end method attributeReplaced()");
*/		/*throw new java.lang.UnsupportedOperationException(
				"Method attributeReplaced () not yet implemented.");*/
	}

	// Notification that a session was created
	public void sessionCreated(HttpSessionEvent se) {
		log.info("begin method sessionCreated()");
		// get the session object 
		HttpSession session = se.getSession(); 
		//HttpSession method getId() returns the sessionid 
		String sid = session.getId();
		//the standard  output will write to cfusionmx\runtime\logs\defaultout. log 
		log.info("Session " + sid + " created");
		log.info("end method sessionCreated()");
		/*throw new java.lang.UnsupportedOperationException(
				"Method sessionCreated () not yet implemented.");*/
	}

	// Notification that a session was invalidated
	public void sessionDestroyed(HttpSessionEvent se) {
		log.info("begin method sessionDestroyed()"); 
		if(ParamerterVfs.idElector!=null){
		log.info("ParamerterVfs.idElector = "+ ParamerterVfs.idElector);
		  ParamerterVfs.servletContext.removeAttribute(ParamerterVfs.idElector); 
		  HttpSession session = se.getSession();
		  String sid = session.getId(); 
		  SessionTracking sessionTracking = new SessionTracking();
		  sessionTracking.setSessionId(sid); 
			try {
				SessionTrackingService.deleteSessionTrackingByPk(sessionTracking);
			} catch (DomainException e) {
				log.error(null,e);
			} 
			log.info("Session " + sid + " destroyed");
		} 
 		log.info("end method sessionDestroyed()");
		/*throw new java.lang.UnsupportedOperationException(
				"Method sessionDestroyed () not yet implemented.");*/
	}

	// Notification that a new attribute has been added to a session
	public void attributeAdded(HttpSessionBindingEvent se) {
		/*log.info("begin method attributeAdded()");
		log.info("end method attributeAdded()");
		*/		/*throw new java.lang.UnsupportedOperationException(
				"Method attributeAdded () not yet implemented.");*/
	}

	// Notification that an attribute has been removed from a session
	public void attributeRemoved(HttpSessionBindingEvent se) {
/*		log.info("begin method attributeRemoved()");
		log.info("end method attributeRemoved()");
*/				/*throw new java.lang.UnsupportedOperationException(
				"Method attributeRemoved () not yet implemented.");*/
	}

	// Notification that an attribute of a session has been replaced
	public void attributeReplaced(HttpSessionBindingEvent se) {
		/*log.info("begin method attributeReplaced()");
		log.info("end method attributeReplaced()");
		*//*throw new java.lang.UnsupportedOperationException(
				"Method attributeReplaced () not yet implemented.");*/
	}

	// Notification that a request was created
	public void requestInitialized(ServletRequestEvent sre) {
	/*	log.info("begin method requestInitialized()");
		log.info("end method requestInitialized()");
	*/	/*throw new java.lang.UnsupportedOperationException(
				"Method requestInitialized () not yet implemented.");*/
	}

	// Notification that a request was invalidated
	public void requestDestroyed(ServletRequestEvent sre) {
/*		log.info("begin method contextDestroyed()");
		log.info("end method contextDestroyed()");
*/		/*	throw new java.lang.UnsupportedOperationException(
				"Method requestDestroyed () not yet implemented.");*/
	}

	// Notification that a new attribute has been added to a request
	public void attributeAdded(ServletRequestAttributeEvent srae) {
	/*	log.info("begin method attributeAdded()");
		log.info("end method attributeAdded()");
	*/		/*throw new java.lang.UnsupportedOperationException(
				"Method attributeAdded () not yet implemented.");*/
	}

	// Notification that an attribute has been removed from a request
	public void attributeRemoved(ServletRequestAttributeEvent srae) {
		/*log.info("begin method attributeRemoved()");
		log.info("end method attributeRemoved()");
		*//*throw new java.lang.UnsupportedOperationException(
				"Method attributeRemoved () not yet implemented.");*/
	}

	// Notification that an attribute of a request has been replaced
	public void attributeReplaced(ServletRequestAttributeEvent srae) {
	/*	log.info("begin method attributeReplaced()");
		log.info("end method attributeReplaced()");
	*/	/*throw new java.lang.UnsupportedOperationException(
				"Method attributeReplaced () not yet implemented.");*/
	}
}
